<?php
$sessionData = SessionData::instance();
if ($sessionData->isFirstMarkPatternCloser('/viSeminar\/.*/', '/.*viRegistration\/viRegistrationList/')) {
    html::printPath(
        array(
            'viSeminar.form-title' => 'admin/viSeminar/all',
            $viSeminar['name'] => 'admin/viSeminar/display?id=' . $viSeminar['id'],
            'viSeminar.viRegistrationList-title' => 'admin/viSeminar/viRegistrationList?id=' . $viSeminar['id'],
            'viRegistration.displayPage-title' => ''
        ),
        'viRegistration.displayPage-desc'
    );
}
else {
    html::printPath(
        array('viRegistration.form-title' => '/admin/viRegistration/all', 'viRegistration.displayPage-title' => ''),
        'viRegistration.displayPage-desc'
    );
}
$msg->printMessages();

echo html::entityInfoIcon('vi_registration', $registration, 'id');

$currencyMark = text::get('currency.mark-' . $optionsForm['currency_code']);

echo '<ul class="commandList" style="margin-top: 0.5em">';
echo '<li class="editLi">' . html::anchor('/admin/viRegistration/edit?id=' . $registration['id'], text::get('viRegistration.editThis')) . '</li>';
if (registrationUtils::canBeCancelled($registration, $viSeminar)) {
    echo '<li class="deleteLi"><a href="#" onclick="return openInDialog(\'cancellationDialog\', \'' . text::getEscaped('myRegistration.cancellation') . '\');">' . text::getEscaped('myRegistration.cancellation') . '</a></li>';
}
echo '</ul>';

// Cancellation dialog
echo '<div id="cancellationDialog" class="hidden">';
echo form::open('/admin/viRegistration/cancel');
echo form::hidden('id', $registration['id']);
if ($registration['paid_price']) {
    echo '<p class="centered">' . text::getEscaped('viRegistration.cancellationOfPaid') . '</p>';
}
else {
    echo '<p class="centered">' . text::getEscaped('viRegistration.cancellationOfNotPaid') . '</p>';
    // echo '<p>' . text::getEscaped('viRegistration.isCancellationChargeApplied');
    // echo form::yesNoComboBox('applyCancellationCharge', sql::$FALSE);
    // echo '</p>';
}
echo '<p class="centered"><button type="submit">' . text::getEscaped('myRegistration.cancelThis') . '</button></p>';
echo form::close();
echo '</div>';

echo '<ul>';
echo '<li>' . text::getEscaped('myRegistration.createDateTime') . ': ' . format::timestamp($registration['created_at']) . '</li>';
if ($registration['cancelled_at']) {
    echo '<li><span class="regIsCancelled">' . text::getEscaped('myRegistration.regIsCancelled', format::timestamp($registration['cancelled_at'])) . '</span></li>';
}
    echo '<li>';
    if ($registration['is_present']) {
        echo text::getEscaped('myRegistration.participantIsPresent');
    }
    else {
        echo text::getEscaped('myRegistration.participantIsNotPresent');
    }
    echo ' ' . html::anchor('/admin/viRegistration/togglePresence?id=' . $registration['id'], text::get('viRegistration.togglePresence') . '.', array('class' => 'description'));
    echo '</li>';
echo '</ul>';

// Payments
$toggledBlockClass = html::printCollapsableBlockHeader('viRegistration.paymentsBlock', FALSE, $this->PAYMENTS_VISIBLE_PROPERTY);
echo '<div class="' . $toggledBlockClass . '">';
echo '<ul class="commandList">';
echo '<li class="addLi">' . html::popupForm('/admin/viRegistration/paymentCreate?viRegistrationId=' . $registration['id'],
        text::get('viRegistration.addNewPayment')) . '</li>';
echo '</ul>';
if (count($paymentList) == 0) {
    echo text::getEscaped('viRegistration.noPayments');
}
else {
    $elb = new EntityListBuilder('viRegistration', $paymentList);
    $elb->setUuid($this->DISPLAY_PAYMENT_LIST_UUID);
    $elb->text('record_number')->linkWithValue('/admin/viRegistration/paymentDisplay?id=', 'id')->openInPopup();
    $elb->decimal('paid_amount')->alignRight();
    $elb->text('created_by_id')->doubleValue('first_name', 'last_name');
    $elb->date('payment_date');
    $elb->time('payment_time');
    $elb->longText('payment_notes')->alignLeft();;
    $elb->write();
    
    // Info about how much the user paid.
    $paidTotal = 0;
    foreach ($paymentList as $payment) {
        $paidTotal = bcadd($paidTotal, $payment['paid_amount'], sql::$DECIMAL_PRECISION);
    }
    echo '<table class="displayTable">';
    echo '<tbody>';
    echo '<tr><th><label>' . text::getEscaped('viRegistration.paidTotal') . '</label></th><td>' . format::decimal($paidTotal) . ' ' . $currencyMark . '</td></tr>';
    echo '<tr><th><label>' . text::getEscaped('viRegistration.total_price') . '</label></th><td>' . format::decimal($registration['total_price']) . ' ' . $currencyMark . '</td></tr>';
    $priceComparison = bccomp($registration['total_price'], $paidTotal, sql::$DECIMAL_PRECISION);
    if ($priceComparison == 1) {
        $priceMinusPaid = bcsub($registration['total_price'], $paidTotal, sql::$DECIMAL_PRECISION);
        echo '<tr><th><label>' . text::getEscaped('viRegistration.underpaidAmount') . '</label></th><td>' . format::decimal($priceMinusPaid) . ' ' . $currencyMark . '</td></tr>';
    }
    else if ($priceComparison == -1) {
        $paidMinusPrice = bcsub($paidTotal, $registration['total_price'], sql::$DECIMAL_PRECISION);
        echo '<tr><th><label>' . text::getEscaped('viRegistration.overpaidAmount') . '</label></th><td>' . format::decimal($paidMinusPrice) . ' ' . $currencyMark . '</td></tr>';
    }
    echo '</tbody>';
    echo '</table>';
}
echo '</div>';

$viRegistrationInfo = new View('/user/viregWizard/fragments/viRegistrationInfo');
$viRegistrationInfo->render(true);